package org.cuit.lmq.handler;


import cn.hutool.http.HttpStatus;
import lombok.extern.slf4j.Slf4j;
import org.cuit.lmq.domain.common.Result;
import org.cuit.lmq.exception.CustomException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Seon
 * @date 2022/11/13 13:22
 */
@Slf4j
@ResponseBody
@ControllerAdvice(annotations = {Controller.class, RestController.class})
public class AppExceptionHandler {
    //    private static final String DUPLICATE_KEY = "Duplicate entry";
//
//    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
//    public Result<?> exceptionHandler(SQLIntegrityConstraintViolationException exception) {
//        log.error(exception.getMessage());
//        if (exception.getMessage().contains(DUPLICATE_KEY)) {
//            String[] split = exception.getMessage().split(" ");
//            String msg = "失败，" + split[2] + "已经存在了，改一个叭";
//            return Result.fail(msg, HttpStatus.HTTP_INTERNAL_ERROR);
//        }
//        return Result.fail("失败，请检查信息", HttpStatus.HTTP_INTERNAL_ERROR);
//    }
    @ExceptionHandler(CustomException.class)
    public Result<?> exceptionHandler(CustomException exception) {
        log.error(exception.getMessage());
        return Result.fail(exception.getMessage(), HttpStatus.HTTP_INTERNAL_ERROR);
    }
}
